<?php
require_once('framework/msc-load.php');
require_once('framework/msc-functions.php');

if(isset($_POST['roomID'])):
    global $db;

    $roomID = $_POST['roomID'];
    $checkin = $_POST['checkin'];
    $checkout = $_POST['checkout'];
    $guests = $_POST['guests'];
    
    $dateFormat = explode('/',$checkin);
    $checkin = $dateFormat[2].'-'.$dateFormat[1].'-'.$dateFormat[0];

    $dateFormat = explode('/',$checkout);
    $checkout = $dateFormat[2].'-'.$dateFormat[1].'-'.$dateFormat[0];

    $query = 'SELECT ID, Guests FROM ROOM_TYPES WHERE `Guests` >= '.$guests. ' AND ID='. $roomID ;
    $result = $db->query($query);
    $available_rooms = array();
    if($result):
       while ($row = $result->fetch_assoc()):
           $query = 'SELECT COUNT(*) FROM AVAILABILITY WHERE `ID_Type` = '.$row['ID'].' AND `Reservation_Number` >= '.$row['Guests'].' AND Date BETWEEN "'.$checkin.'" AND "'.$checkout.'"';
           $result2 = $db->query($query);
           $row2 = $result2->fetch_row();
           if($row2[0] == 0){
              echo 1;
              return;
           }else{
             echo 0;
              return;  
           }
       endwhile;
    endif;

    echo -1;
    return ;
    
endif;

?>